package algorithm;

import java.util.ArrayList;
import java.util.List;

public class leetcode_0216 {
    public List<List<Integer>> combinationSum3(int k, int n) {
        List<List<Integer>> list=new ArrayList<>();
        List<Integer> path=new ArrayList<>();
        backtracking(list,path,n,k,1,1);
        return list;
    }
    public void backtracking(List<List<Integer>> list,List<Integer> path,int n,int k,int num,int begin){
        if(n<0) return;
        if(n==0&&num==k+1){
            list.add(new ArrayList<>(path));
            return;
        }
        if(num==k+1) return;
        for(int i=begin;i<=9;++i){
            path.add(i);
            backtracking(list,path,n-i,k,num+1,i+1);
            path.remove(path.size()-1);
        }
    }
}
